﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _3DFileConverter
{
    class ObjToStlConverter
    {
        uint numberOfFacets = 0;

        public uint NumberOfFacets
        {
            get
            {
                return numberOfFacets;
            }

            set
            {
                numberOfFacets = value;
            }
        }

        public void facetCalculation(string[] vertecData) {

            List<int> elements = stringProcessing(vertecData);

            int triangleCount = elements.Count() - 2;

            for (int i = 0; i < triangleCount; i++){

                numberOfFacets++;
            }
        }

        List<int> stringProcessing(string[] dataToProc) {

            List<int>listOfVerteces = new List<int>();

            foreach (string element in dataToProc){

                if (element != "f") {
                    string[] vertecProperties = element.Split('/');
                    listOfVerteces.Add(int.Parse(vertecProperties[0]));
                }
            }

            return listOfVerteces;

        }

        public Tuple<float, float>convertFromTo(ObjFigure objFigureData, string[] vertecData, 
                    float[] scaleData, float[] translateData) {

            StlWriter stlObjWriter = new StlWriter();

            float area = 0;
            float volume = 0;

            List<int> elements = stringProcessing(vertecData);

            int triangleCount = elements.Count() - 2;

            for (int i = 0; i < triangleCount; i++) {

                StlFigure stlFigure = new StlFigure();

                StlFigure newStlFigure = new StlFigure();

                stlFigure.Verteces.Add(objFigureData.Verteces.ElementAt(elements.ElementAt(0) - 1));

                stlFigure.Verteces.Add(objFigureData.Verteces.ElementAt(elements.ElementAt(i + 1) - 1));

                stlFigure.Verteces.Add(objFigureData.Verteces.ElementAt(elements.ElementAt(i + 2) - 1));

                stlObjWriter.stlFormatWrite(stlFigure);

                area += stlFigure.calculateFacetArea();
                volume += stlFigure.calculateFacetVolume();
            }

            return Tuple.Create(area, volume);
        }
    }
}
